//
//  OauthModel.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/19.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

/// 用户信息模型
class OauthModel: NSObject, NSCoding {

    /// 用于调用access_token，接口获取授权后的access token。
    var access_token:String?
    /// access_token的生命周期，单位是秒数。
    var expires_in:NSTimeInterval = 0{
        
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    /// 令牌过期具体时间
    var expires_date:NSDate?
    /// 当前授权用户的UID
    var uid:String?
    /// 用户昵称
    var screen_name:String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large:String?
    
    init(dict:[String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String{
        
        let keys = ["access_token", "expires_date", "uid", "screen_name", "avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //MARK: - 归档方法
    // 归档方法
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")

    }
    
    // 解档方法
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
